package com.gainloop.Api;

import java.util.ArrayList;

import org.alexd.jsonrpc.JSONRPCException;
import org.json.JSONArray;
import org.json.JSONException;

import com.gainloop.Api.Entity.User;
import com.gainloop.Api.Entity.UserFriend;

public class UserFriends extends ApiAbstract {


	public ApiResponseObject<User> addFbFriends(User user, ArrayList<Long> friendFbId) throws JSONRPCException
	{
		if(user.getId() == 0) {
			throw new ApiException("user id must be set");
		}

		JSONArray fbIds = new JSONArray();
		for (Long fbId : friendFbId) {
			fbIds.put(fbId.longValue());
		}
		
		try {
			return new ApiResponseObject<User>(GetClient().callJSONObject("user_friends.addFbFriends", user.getId(), fbIds), User.class);
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}
	
	public UserFriend getUserFriend(User user, long friendId) throws JSONRPCException
	{
		if(user.getId() == 0) {
			throw new ApiException("user id must be set");
		}
		if(friendId <= 0) {
			throw new ApiException("friendId must be set");
		}
		
		try {
			ApiResponseObject<UserFriend> res = new ApiResponseObject<UserFriend>(GetClient().callJSONObject("user_friends.getUserFriend", user.getId(), friendId), UserFriend.class);
			
			if(res.getReturnCode() != 0) {
				throw new ApiException("wrong usage: ", res);
			}
			
			return res.getData();
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}
	
	public ArrayList<UserFriend> getAllByUser(User user, int limit) throws JSONRPCException
	{
		if(user.getId() == 0) {
			throw new ApiException("user id must be set");
		}
		if(limit < 1) {
			throw new ApiException("limit must be positive");
		}
		if(limit > 200) {
			throw new ApiException("limit must be 200 maximum");
		}
		
		try {
			ApiResponseArray<UserFriend> res = new ApiResponseArray<UserFriend>(GetClient().callJSONObject("user_friends.getAllByUser", user.getId(), limit), UserFriend.class);
			
			if(res.getReturnCode() != 0) {
				throw new ApiException("wrong usage: ", res);
			}
			
			return res.getData();
			
		} catch (JSONException e) {
			throw new ApiException("invalid respond");
		}
	}
	
}
